package com.wm.blog_config.config;


import com.wm.blog_common.exception.BusinessException;
import com.wm.blog_common.exception.CommonException;
import com.wm.blog_common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author wm_yu
 * @date 2020/04/15
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = {CommonException.class, BusinessException.class})
    @ResponseBody
    public Result bizExceptionHandler(Exception e){
        String msg = "业务异常:%s";
        log.error("业务异常:{}",String.format(msg,e.getMessage()),e);
        return Result.failure(String.format(msg,e.getMessage()));
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result otherException(Exception e){
        String msg = "未知错误:%s";
        log.error("未知错误:{}",String.format(msg,e.getMessage()),e);
        return Result.failure(String.format(msg,e.getMessage()));
    }


}
